﻿
using ERP.Application.Goods.GoodsDto;
using ERP.Domain.Entities;
using ERP.Repositories;

using Microsoft.Extensions.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ERP.Application.Goods
{
    /// <summary>
    /// 商品实现
    /// </summary>
    public class GoodDataService
    {
        /// <summary>
        /// 商品类型仓储
        /// </summary>
        private readonly IRepository<GoodType> goodTypeRep;
        /// <summary>
        /// 商品仓储
        /// </summary>
        private readonly IRepository<Good> goodRep;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<GoodDataService> logger;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="goodTypeRep">商品类型仓储</param>
        /// <param name="goodRep">商品仓储</param>
        public GoodDataService(IRepository<GoodType> goodTypeRep, IRepository<Good> goodRep, ILogger<GoodDataService> logger)
        {
            this.goodTypeRep = goodTypeRep;
            this.goodRep = goodRep;
            this.logger = logger;
        }
        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="keywords"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        public object GetGoods(int categoryId, string? keywords, string? color, int pageSize, int pageIndex)
        {

            //var list = await goodRep.GetValueAsync(x => x.CategoryId == categoryId || (x.Barcode.Contains(keywords) || x.ProductName.Contains(keywords) || x.Standard.Contains(keywords) || x.ProductModel.Contains(keywords)) || x.ProductColor == color);
            var list = goodRep.GetAll();
            if (categoryId != 0)
            {
                list = list.Where(x => x.CategoryId == categoryId);
            }
            if (!string.IsNullOrEmpty(keywords))
            {
                list = list.Where(x => x.Barcode.Contains(keywords) || x.ProductName.Contains(keywords) || x.Standard.Contains(keywords) || x.ProductModel.Contains(keywords));
            }
            if (!string.IsNullOrEmpty(color))
            {
                list = list.Where(x => x.ProductColor == color);
            }
            return new
            {
                listCount = list.Count(),
                pageCount = (int)Math.Ceiling(list.Count() * 1.0 / pageSize),
                list = list.OrderBy(x => x.ProductId).Skip((pageIndex - 1) / pageSize).Take(pageSize).ToList()
            };
        }
        /// <summary>
        /// 获取商品类别信息
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns>返回商品类别信息</returns>
        public List<CasCader> GetCasCaders()
        {
            try
            {
                List<CasCader> list = new List<CasCader>();
                var res = goodTypeRep.GetAll().ToList();
                var topRes = res.Where(x => x.ParentId == 0).ToList();
                //var res = goodTypeRep.GetValueAsync(x => x.ParentId == 0);
                foreach (var item in topRes)
                {
                    CasCader cascader = new CasCader();
                    cascader.Value = item.GoodTypeId;
                    cascader.Label = item.TypeName;
                    list.Add(cascader);
                    SetGoodType(res, cascader);
                }
                return list;
            }
            catch (Exception exp)
            {
                logger.LogError("获取商品类别信息出错" + exp.Message);
                throw;
            }
        }
        /// <summary>
        /// 递归设置级联
        /// </summary>
        /// <param name="goodTypes"></param>
        /// <param name="casCader"></param>
        public void SetGoodType(List<GoodType> goodTypes, CasCader casCader)
        {
            try
            {
                var res = goodTypes.Where(x => x.ParentId == casCader.Value).ToList();
                foreach (var item in res)
                {
                    CasCader casCader1 = new CasCader();
                    casCader1.Value = item.GoodTypeId;
                    casCader1.Label = item.TypeName;
                    casCader.Children.Add(casCader1);
                    SetGoodType(goodTypes, casCader1);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加商品分类
        /// </summary>
        /// <param name="goodType"></param>
        /// <returns>返回商品响应</returns>
        public async Task<AddGoodTypeResponseDto> AddGoodType(AddGoodTypeRequestDto goodType)
        {
            try
            {
                AddGoodTypeResponseDto response = new AddGoodTypeResponseDto();
                response.Code = -1;
                response.Message = "添加分类失败";
                GoodType type = new GoodType();
                type.TypeName = goodType.TypeName;
                type.ParentId = goodType.ParentId;
                var res = await goodTypeRep.AddAsync(type);
                response.Code = 1;
                response.Message = "添加分类成功";
                return response;

            }
            catch (Exception exp)
            {
                logger.LogError("添加商品分类出错" + exp.Message);
                throw;
            }
        }



    }
}
